//
//  UserCenterTableViewController.m
//  OrderMeal
//
//  Created by 江苏宏创 on 14-7-21.
//  Copyright (c) 2014年 qkong. All rights reserved.
//

#import "UserCenterTableViewController.h"
#import "Protocols.h"
#import "SendHttpOperation.h"
#import "defineFile.h"
@interface UserCenterTableViewController ()<LoginProtocol,UITextFieldDelegate>

@end

@implementation UserCenterTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.username.delegate = self;
    self.password.delegate = self;
    [SendHttpOperation sharedHttpOperation].loginDelegate = self;
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //如果用户名和密码存在
    if ([userDefaults objectForKey:@"username"] && [userDefaults objectForKey:@"password"]) {
        self.username.text = [userDefaults objectForKey:@"username"];
        self.password.text = [userDefaults objectForKey:@"password"];
    }
}

-(void)loginSuccess{
    [self performSegueWithIdentifier:@"登录" sender:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)loginAction:(id)sender {
    [self.view endEditing:YES];
    if ([self checkInput]) {
        [[SendHttpOperation sharedHttpOperation] loginWithUsername:self.username.text Password:self.password.text];
    }
}


- (IBAction)registAction:(id)sender {
    [self performSegueWithIdentifier:@"注册" sender:nil];
}

-(BOOL)checkInput{
    if (self.username.text.length == 0) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"errorMsg" object:nil userInfo:@{@"ERROR":@"用户名不能为空"}];
        return NO;
    }else if (self.password.text.length == 0){
       [[NSNotificationCenter defaultCenter]postNotificationName:@"errorMsg" object:nil userInfo:@{@"ERROR":@"密码不能为空"}];
        return NO;
    }else{
        return YES;
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    if (textField == self.username) {
        [self.password becomeFirstResponder];
    }
    
    return YES;
}


@end
